# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=pgmodeler
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-plugins")
pkgver=1.2.0
_plugsha=a7baf113deec9888f22fc91ffca740562a02ae57
pkgrel=1
pkgdesc="PostgreSQL Database Modeler: an open source CASE tool for modeling PostgreSQL databases (mingw-w64)"
url="https://pgmodeler.io"
msys2_repository_url="https://github.com/pgmodeler/pgmodeler"
license=('GPL3')
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
depends=("${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-postgresql"
         "${MINGW_PACKAGE_PREFIX}-qt6-svg")
optdepends=("${MINGW_PACKAGE_PREFIX}-${_realname}-plugins: official plugins")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools")

source=("${msys2_repository_url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "https://github.com/pgmodeler/plugins/archive/${_plugsha}/plugins-${_plugsha}.tar.gz"
        '0001-pgmodeler-1.0.2-libgui-inc.patch'
        '0002-pgmodeler-1.0.2-no-check-update.patch'
        '0003-pgmodeler-1.0.2-fix-global-attributes.patch')
sha256sums=('66d1cc441a5247edb3fe9b5f09fbc480023955da037cd09a5c8811d3caa7f270'
            'c5bfe5e14a22f9004993d61f16bee423dce7564c609f47f174dfa2cedaec9c8f'
            'f658ed0129b0402c8d91b44f62498e35f9c1af87ae14ae1a4030f0b9e7b8bf78'
            '38747cc62c068e1597a10661ceb750e7545ffcd9125ba9b237f663374e4b8a2b'
            'd902b82a0a397380c7b61327dd67fd6d6f2c50351d2b1fa480bc80b1ab47546d')

prepare() {
    cd "${srcdir}"/${_realname}-${pkgver}

    patch -Np1 -i "${srcdir}"/0001-pgmodeler-1.0.2-libgui-inc.patch
    patch -Np1 -i "${srcdir}"/0002-pgmodeler-1.0.2-no-check-update.patch
    patch -Np1 -i "${srcdir}"/0003-pgmodeler-1.0.2-fix-global-attributes.patch

    cp -r "${srcdir}"/plugins-${_plugsha} "${srcdir}"/${_realname}-${pkgver}/plugins
}

build() {
    msg2 "Will be in future"
}

package_pgmodeler() {
    cd "${srcdir}"/${_realname}-${pkgver}

    "${MINGW_PREFIX}"/bin/qmake6.exe CONFIG+=release \
          PREFIX="${pkgdir}${MINGW_PREFIX}" \
          BINDIR="${pkgdir}${MINGW_PREFIX}"/bin \
          PRIVATEBINDIR="${pkgdir}${MINGW_PREFIX}"/bin \
          PRIVATELIBDIR="${pkgdir}${MINGW_PREFIX}"/bin \
          PLUGINSDIR="${pkgdir}${MINGW_PREFIX}"/lib/pgmodeler/plugins \
          CONFDIR="${pkgdir}${MINGW_PREFIX}"/etc/pgmodeler \
          SHAREDIR="${pkgdir}${MINGW_PREFIX}"/share/pgmodeler \
          DOCDIR="${pkgdir}${MINGW_PREFIX}"/share/doc/pgmodeler \
          LANGDIR="${pkgdir}${MINGW_PREFIX}"/share/pgmodeler/lang \
          SAMPLESDIR="${pkgdir}${MINGW_PREFIX}"/share/pgmodeler/samples \
          SCHEMASDIR="${pkgdir}${MINGW_PREFIX}"/share/pgmodeler/schemas \
          PGSQL_LIB="${MINGW_PREFIX}"/bin/libpq.dll \
          PGSQL_INC="${MINGW_PREFIX}"/include \
          XML_INC="${MINGW_PREFIX}"/include/libxml2 \
          XML_LIB="${MINGW_PREFIX}"/bin/libxml2-2.dll \
          NO_UPDATE_CHECK=MSYS2doesthis \
          QMAKE_CXXFLAGS_RELEASE+="${CXXFLAGS} ${CPPFLAGS}" \
          pgmodeler.pro

    make

    make INSTALL_ROOT="" install

    rm "${pkgdir}${MINGW_PREFIX}/bin/"lib*.dll.a
    rm "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/"{LICENSE,README.md}

    install -Dm644 LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

    # split plugins
    rm -rf "dest${MINGW_PREFIX}" && mkdir -p "dest${MINGW_PREFIX}"
    mv "${pkgdir}${MINGW_PREFIX}/lib" "dest${MINGW_PREFIX}/lib"
}

package_pgmodeler-plugins() {
  cd "${srcdir}"/${_realname}-${pkgver}

  pkgdesc+=" (plugins)"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}")
  optdepends=()

  mv dest/* "${pkgdir}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;

